package com.killer.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author ：Killer
 * @date ：Created in 20-12-24 上午11:46
 * @description：${description}
 * @modified By：
 * @version: version
 */
@RestController
@RequestMapping("/autodeliver")
//允许手动向Client客户端发起POST请求,http://localhost:8092/actuator/refresh,刷新配置信息
//重要的是@RefreshScope 需要加在 @Value的类上面，并不是启动类上面
@RefreshScope
public class AutodeliverController {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;

    @Value("${killer.message}")
    private String message;

    @Value("${mysql.url}")
    private String url;

    @GetMapping("/getConfig")
    public String getConfig() {
        return message+"========"+url;
    }

    @GetMapping("/checkState/{userId}")
    public Integer findResumeOpenState(@PathVariable Long userId) {

        List<ServiceInstance> instances = discoveryClient.getInstances("cloud1-resume");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getHost()+":"+instance.getPort());
        }
        //这里不可以这样调用，这样会报java.net.UnknownHostException: cloud1-resume异常
//        Integer forObject = restTemplate.getForObject("http://cloud1-resume/resume/openstate/"
//                        + userId, Integer.class);
        //直接获取第一台
        ServiceInstance instance = instances.get(0);
        Integer forObject = restTemplate.getForObject("http://"+instance.getHost()+":"+instance.getPort()+"/resume/openstate/"
                        + userId, Integer.class);
        System.out.println("======>>>调用简历微服务,获取到用户" +
                userId + "的默认简历当前状态为:" + forObject);
        return forObject;
    }

    @GetMapping("/checkStateRibbon/{userId}")
    public Integer findResumeOpenStateRibbon(@PathVariable Long userId) {
        Integer forObject = restTemplate.getForObject("http://cloud1-resume/resume/openstate/"
                + userId, Integer.class);
        System.out.println("======>>>调用简历微服务,获取到用户" +
                userId + "的默认简历当前状态为:" + forObject);
        return forObject;
    }

    @GetMapping("/checkStateTimeOut/{userId}")
    @HystrixCommand(threadPoolKey = "findResumeOpenStateTimeOut",
            threadPoolProperties = {
                    //线程数
                    @HystrixProperty(name="coreSize",value = "1"),
                    //等待队列
                    @HystrixProperty(name="maxQueueSize",value="20")},
            commandProperties = {
            // 每一个属性都是一个HystrixProperty
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="2000")})
    public Integer findResumeOpenStateTimeOut(@PathVariable Long userId) {
        System.out.println(Thread.currentThread().getId());
        Integer forObject=0;
        try {
            forObject= restTemplate.getForObject("http://cloud1-resume/resume/openstate/"
                    + userId, Integer.class);

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(Thread.currentThread().getId());
        }

        System.out.println("======>>>调用简历微服务,获取到用户" +
                userId + "的默认简历当前状态为:" + forObject);
        if (forObject == 8081) {
            System.out.println(Thread.currentThread().getId());
        }
        return forObject;
    }

    @GetMapping("/checkStateTimeOutFallBack/{userId}")
    @HystrixCommand(
            threadPoolKey = "findResumeOpenStateTimeOutFallBack",
            threadPoolProperties = {@HystrixProperty(name="coreSize",value = "2"),
                                @HystrixProperty(name="maxQueueSize",value="20")},
            commandProperties = {
            // 每一个属性都是一个HystrixProperty
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="2000")}
            ,fallbackMethod = "myFallBack")
    public Integer findResumeOpenStateTimeOutFallBack(@PathVariable Long userId) {
        Integer forObject = restTemplate.getForObject("http://cloud1-resume/resume/openstate/"
                + userId, Integer.class);
        System.out.println("======>>>调用简历微服务,获取到用户" +
                userId + "的默认简历当前状态为:" + forObject);
        return forObject;
    }


    /*
    定义回退方法,返回预设默认值
    注意:该方法形参和返回值与原始方法保持一致
    */
    public Integer myFallBack(Long userId) {
        return -1; // 兜底数据
    }
}
